package com.example.restatr.Atcivity.JobSearch;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.example.restatr.Atcivity.Main.MainActivity;
import com.example.restatr.Atcivity.JobSearch.Bean.JobDeliverBean;
import com.example.restatr.R;
import com.example.restatr.Tools;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

public class JobDeliverList extends AppCompatActivity {
    private Toolbar tb;
    private TextView barTitle;
    private EditText search;
    private ListView deliverList;
    private LinearLayout Null;
    private List<JobDeliverBean.RowsBean> deliverBeans = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_job_deliver_list);

        tb = findViewById(R.id.toolbar);
        barTitle = findViewById(R.id.bar_Title);
        search = findViewById(R.id.search);
        deliverList = findViewById(R.id.deliverList);
        Null = findViewById(R.id.Null);

        Tools.SetBarTitle(tb, barTitle, "投递历史", search, this,false);
        GetDeliverList();
    }

    private void GetDeliverList() {
        new Thread(() -> {
            String token = Tools.Get("/prod-api/api/job/deliver/list?userId=" + MainActivity.sp.getInt("userId", 0), MainActivity.sp.getString("token", ""));
            JobDeliverBean jobDeliverBean = new Gson().fromJson(token, JobDeliverBean.class);
            deliverBeans.addAll(jobDeliverBean.getRows());
            runOnUiThread(() -> {
                if (deliverBeans.size() > 0) {
                    deliverList.setVisibility(View.VISIBLE);
                    Null.setVisibility(View.GONE);
                    deliverList.setAdapter(new JobDeliverListAdapter());
                } else {
                    deliverList.setVisibility(View.GONE);
                    Null.setVisibility(View.VISIBLE);
                }
            });
        }).start();
    }

    class JobDeliverListAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return deliverBeans.size();
        }

        @Override
        public Object getItem(int position) {
            return deliverBeans.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = getLayoutInflater().inflate(R.layout.job_item_layout, null);
            TextView professionName = convertView.findViewById(R.id.professionName);
            TextView salary = convertView.findViewById(R.id.salary);
            TextView need = convertView.findViewById(R.id.need);
            TextView companyName = convertView.findViewById(R.id.companyName);
            TextView applyFor = convertView.findViewById(R.id.applyFor);
            applyFor.setVisibility(View.GONE);

            professionName.setText(deliverBeans.get(position).getPostName());
            salary.setText(deliverBeans.get(position).getMoney() + "元/月");
            need.setText(deliverBeans.get(position).getSatrTime());
            companyName.setText(deliverBeans.get(position).getCompanyName());
            return convertView;
        }
    }

}